package math
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.utils.*;
	
	import math.base.BaseObject;
	import math.base.IEffect;

	public class CircleImage extends BaseObject implements IEffect
	{
		private var list:Array=new Array  ;//管理列表
		private var angle:Number;//分布角度
		private var perangle:Number;//偏移角度
		private var num:int;
		private var count:int;//统计
		private var colletion:Array=new Array();//用于记录图片
		public function CircleImage(num:uint,R:Number)
		{
			this.num=num;//图片个数
			super.R=R;
			this.angle=2*Math.PI/num;
			this.perangle=(num-2)*Math.PI/num;
		}
		
		//添加一些库连接出来的图片id
		public function add(className:String):void
		{
			var myClass:Object=getDefinitionByName(className);//怎样反射
			var myobj:MovieClip=new myClass() as MovieClip;
			colletion.push(myobj);			
		}
		
		//加载一个xml格式
		public function loadXML(path:String):void
		{
			var loader:URLLoader=new URLLoader();
			loader.addEventListener(Event.COMPLETE,onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR,onError);
			loader.load(new URLRequest(path));			
		}
		
		private function onComplete(event:Event):void
		{   
			var xml:XML=XML(event.target.data);
			event.currentTarget.removeEventListener(Event.COMPLETE,onComplete);			
			//适合一种指定xml格式;
			count=xml.node.length();
			//加载多张图片
			for(var i:int=0;i<count;i++)
			{
				var loader:Loader=new Loader();
				loader.load(new URLRequest(xml.node.url[i].toString()));
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);	
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);			
			}			
		}
		
		//图片加载完毕
		private function completeHandler(event:Event):void
		{
			var bitmap:Bitmap=event.currentTarget.content;
			bitmap.x=-bitmap.width/2;
			bitmap.y=-bitmap.height/2;//偏移注册点
			var bmpCanvas:MovieClip=new MovieClip();
			bmpCanvas.addChild(bitmap);
			colletion.push(bmpCanvas);
			
			event.currentTarget.removeEventListener(Event.COMPLETE,completeHandler);
			event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onError);			
			count--;
			if(count==0)
			{  
				this.dispatchEvent(new Event("Finish"));//监听图片完成			   
			}
			
		}
		
		private function onError(event:IOErrorEvent):void
		{
			throw new Error("发生错误");			
		}			
		//创建特效
		public function creatEffect():void
		{  			
			
			for (var j:int=0; j<num; j++)
			{
				var myClass:Object=getDefinitionByName("flash.display.MovieClip");//怎样反射
				var myobj:MovieClip=new myClass() as MovieClip;
				myobj.addChild(colletion[j]);
				trace(myobj);
				myobj.addEventListener(MouseEvent.CLICK,onClick);
				myobj.addEventListener(MouseEvent.MOUSE_OVER,onOver);
				myobj.addEventListener(MouseEvent.MOUSE_OUT,onOut);
				addChild(myobj);
				list.push(myobj);
				myobj.x=R*Math.cos(j*angle);//采用圆的轨迹分布对圆进行分布
				myobj.z=R*Math.sin(j*angle);
				myobj.rotationY=j*perangle*180/Math.PI+90;//偏移角度计算
			}
		}
		
		public function sortZ():void
		{
			list.sort(depthSort);
			
			for (var i:int=0; i<list.length; i++)
			{
				var myimage:MovieClip=list[i];
				this.setChildIndex(myimage,i);
			}
		}
		//处理交互事件
		private function onClick(event:MouseEvent):void
		{
			this.dispatchEvent(new Event("onClick"));
		}
		private function onOver(event:MouseEvent):void
		{
			this.dispatchEvent(new Event("onOver"));
		}
		private function onOut(event:MouseEvent):void
		{
			this.dispatchEvent(new Event("onOut"));
		}
		
	}
}

//import flash.events.Event;
//class 

